﻿SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
GO
CREATE TABLE dbo.bht_PayU_PaymentTransactionError
	(
	ErrorLogId int NOT NULL IDENTITY (1, 1),
	[Transaction] int NOT NULL,
	Error int NOT NULL,
	LogTime datetime NOT NULL
	)  ON [PRIMARY]
GO

ALTER TABLE dbo.bht_PayU_PaymentTransactionError ADD CONSTRAINT
	DF_bht_PayU_PaymentTransactionError_LogTime DEFAULT GETDATE() FOR LogTime
GO
ALTER TABLE dbo.bht_PayU_PaymentTransactionError ADD CONSTRAINT
	PK_bht_PayU_PaymentTransactionError PRIMARY KEY CLUSTERED 
	(
	ErrorLogId
	) ON [PRIMARY]

GO
ALTER TABLE dbo.bht_PayU_PaymentTransactionError ADD CONSTRAINT
	FK_bht_PayU_PaymentTransactionError_bht_PayU_PaymentTransaction FOREIGN KEY
	(
	[Transaction]
	) REFERENCES dbo.bht_PayU_PaymentTransaction
	(
	TransactionId
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.bht_PayU_PaymentTransactionError ADD CONSTRAINT
	FK_bht_PayU_PaymentTransactionError_bht_PayU_PaymentErrors FOREIGN KEY
	(
	Error
	) REFERENCES dbo.bht_PayU_PaymentErrors
	(
	ErrorId
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	

GO
ALTER TABLE dbo.bht_PayU_PaymentTransaction ADD
	SessionId nvarchar(50) NOT NULL
GO
ALTER PROCEDURE [dbo].[bhp_PayU_PaymentTransactionSaveNew] (
	@PosId int,
	@PosAuthKey nchar(7),
	@PaymentType nvarchar(10),
	@ChargeValue numeric(12, 2),
	@Description nvarchar(50),
	@FirstName nvarchar(100),
	@LastName nvarchar(100),
	@EMail nvarchar(100),
	@ClientIP nvarchar(15),
	@Status int,
	@OrderID int,
	@Description2 nvarchar(1024),
	@DescriptionBank nvarchar(27),
	@Street nvarchar(10),
	@StreetNumber nvarchar(10),
	@FlatNumber nvarchar(10),
	@City nvarchar(100),
	@PostCode nvarchar(20),
	@Country nvarchar(100),
	@Phone nvarchar(100),
	@Language char(10),
	@JavaScript bit,
	@PaybackLogin nvarchar(40),
	@Sig nvarchar(32),
	@Ts nvarchar(1000),
	@SessionId nvarchar(50)
)
AS
BEGIN
	INSERT INTO bht_PayU_PaymentTransaction (
		[PosId],
		[PosAuthKey],
		[PaymentType],
		[ChargeValue],
		[Description],
		[FirstName],
		[LastName],
		[EMail],
		[ClientIP],
		[Status],
		[OrderID],
		[Description2],
		[DescriptionBank],
		[Street],
		[StreetNumber],
		[FlatNumber],
		[City],
		[PostCode],
		[Country],
		[Phone],
		[Language],
		[JavaScript],
		[PaybackLogin],
		[Sig],
		[Ts],
		[SessionId]
	) VALUES (
		@PosId,
		@PosAuthKey,
		@PaymentType,
		@ChargeValue,
		@Description,
		@FirstName,
		@LastName,
		@EMail,
		@ClientIP,
		@Status,
		@OrderID,
		@Description2,
		@DescriptionBank,
		@Street,
		@StreetNumber,
		@FlatNumber,
		@City,
		@PostCode,
		@Country,
		@Phone,
		@Language,
		@JavaScript,
		@PaybackLogin,
		@Sig,
		@Ts,
		@SessionId
	)
	
	SELECT @@IDENTITY as TransactionId
END
GO
CREATE PROCEDURE [dbo].[bhp_PayU_PaymentTransactionFind] (
	@PosId int,
	@SessionId nvarchar(50)
) 
AS
BEGIN
	SELECT TOP 1
		*
	FROM
		bht_PayU_PaymentTransaction
	WHERE
		PosId = @PosId
		AND SessionId = @SessionId

END
GO
CREATE PROCEDURE [dbo].[bhp_PayU_PaymentErrorGet] (
	@ErrorId int
) 
AS
BEGIN
	SELECT TOP 1
		*
	FROM
		bht_PayU_PaymentErrors
	WHERE
		ErrorId = @ErrorId

END
GO
CREATE PROCEDURE [dbo].[bhp_PayU_PaymentTransactionLogError] (
	@TransactionId	int,
	@ErrorId int
) 
AS
BEGIN
	INSERT INTO bht_PayU_PaymentTransactionError ([Transaction], [Error]) VALUES (@TransactionId, @ErrorId)

END